﻿using Peak.Can.Basic;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PcanUdsTool.Pcan;

namespace PcanUdsTool
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        List<string> pcanList;
        PCANManage pCAN = new PCANManage();
        /// <summary>
        /// Stores the status of received messages for its display
        /// </summary>
        private ObservableCollection<MessageStatus> canMsgsList = new ObservableCollection<MessageStatus>();
        public MainWindow()
        {
            InitializeComponent();

            pCAN.RecvCANMsgEvent += RecvCanMsgHandle;

            pcanList = pCAN.GetPcanList();
            
            foreach (var item in pcanList)
            {
                ComboBox_DevList.Items.Add(item);
            }
            if (ComboBox_DevList.Items.Count > 0)
            {
                ComboBox_DevList.SelectedIndex = 0;
            }
            ListView_CanMsg.ItemsSource = canMsgsList;
        }

        /// <summary>
        /// Inserts a new entry for a new message in the Message-ListView
        /// </summary>
        /// <param name="newMsg">The messasge to be inserted</param>
        /// <param name="timeStamp">The Timesamp of the new message</param>
        private void InsertMsgEntry(TPCANMsgFD newMsg, UInt64 timeStamp)
        {
            MessageStatus msgStsCurrentMsg;

            lock (canMsgsList)
            {
                // We add this status in the last message list
                //
                msgStsCurrentMsg = new MessageStatus(newMsg, timeStamp, canMsgsList.Count);
                msgStsCurrentMsg.ShowingPeriod = true;
                canMsgsList.Add(msgStsCurrentMsg);
            }
        }
        void RecvCanMsgHandle(TPCANMsgFD theMsg, UInt64 itsTimeStamp)
        {
            App.Current.Dispatcher.Invoke((Action)(() =>
            {
                // We search if a message (Same ID and Type) is 
                // already received or if this is a new message
                //
                lock (canMsgsList)
                {
                    foreach (MessageStatus msg in canMsgsList)
                    {
                        if ((msg.CANMsg.ID == theMsg.ID) && (msg.CANMsg.MSGTYPE == theMsg.MSGTYPE))
                        {
                            // Modify the message and exit
                            //
                            msg.Update(theMsg, itsTimeStamp);
                            return;
                        }
                    }
                    // Message not found. It will created
                    //
                    InsertMsgEntry(theMsg, itsTimeStamp);
                }
            }));
        }
        

        private void Button_Connect_Click(object sender, RoutedEventArgs e)
        {
            if (pCAN.isConnected)
            {
                pCAN.Disconnect();
                Button_Connect.Content = "连接";
            }
            else if(ComboBox_DevList.SelectedIndex!=-1)
            {
                pCAN.Connect(ComboBox_DevList.SelectedIndex);
                Button_Connect.Content = "已连接";
            }
        }
    }
}
